package cn.edu.scau.dormitory_management_system.module.examine.controller;

import cn.edu.scau.dormitory_management_system.aop.annotation.MarkDown;
import cn.edu.scau.dormitory_management_system.constant.Consts;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.CheckinAuditEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.VO.CheckinAuditVO;
import cn.edu.scau.dormitory_management_system.module.examine.service.CheckinApplyService;
import cn.edu.scau.dormitory_management_system.module.examine.service.CheckinAuditService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：Vizzk
 * @description：TODO
 * @date ：2021/8/9 14:23
 */
@RestController
@RequestMapping("/admin/checkin/audit")
public class CheckinAuditController {
    @Autowired
    CheckinApplyService checkinApplyService;
    @Autowired
    CheckinAuditService checkinAuditService;

    @PostMapping("/insertDeptAudit")
    public Object insertDeptAudit(HttpServletRequest request, @RequestBody CheckinAuditEntity auditEntity){
        JSONObject jsonObject = new JSONObject();

        UserEntity user = (UserEntity) request.getAttribute("admin");
        auditEntity.setUserId(user.getUserId());
        auditEntity.setUserName(user.getUserName());
        try {
            checkinApplyService.insertDeptAudit(auditEntity);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "审核完成");
        }
        catch (Exception e){
            e.printStackTrace();
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }

    @MarkDown
    @PostMapping("/insertCollegeAudit")
    public Object insertInCollegeAudit(HttpServletRequest request, @RequestBody CheckinAuditEntity auditEntity){
        JSONObject jsonObject = new JSONObject();

        UserEntity user = (UserEntity) request.getAttribute("admin");
        auditEntity.setUserId(user.getUserId());
        auditEntity.setUserName(user.getUserName());
        try {
            checkinApplyService.insertCollegeAudit(auditEntity);
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "审核完成");
        }
        catch (Exception e){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }

    @GetMapping("/listAuditsByCheckinId")
    public Object listAuditsByCheckinId(Integer checkinId){
        JSONObject jsonObject = new JSONObject();
        try {
            //List<CheckinAuditEntity> audits = checkinAuditService.listAuditsByCheckinId(checkinId);
            List<CheckinAuditVO> audits = checkinAuditService.listAuditVOByCheckinId(checkinId);
            jsonObject.put(Consts.CODE, 200);
            jsonObject.put(Consts.MSG, "查询成功");
            jsonObject.put("audits",audits);
        }
        catch (Exception e){
            e.printStackTrace();
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "查询失败");
        }
        return jsonObject;
    }
}
